using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Employers_register : System.Web.UI.Page
{
    string connectionString = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString =
                            WebConfigurationManager.ConnectionStrings[
                            "Database2000"].ConnectionString;
        if (Session["rcuserid"] == null)
        {
            lblHeader.Text = "New Registration";
            this.Title = "::New Registration";
            literalMessage.Visible = true;
            trTermofService.Visible = true;
            trAgree.Visible = true;
            txtEmail.Enabled = true;
            btnSave.Text = "I Agree";

            Page.ClientScript.RegisterClientScriptBlock(
                    this.GetType(), "ClientScriptFunctionAgree",
                    ClientScriptFunctionAgree(), true);
            btnSave.Attributes.Add("onclick", "return validateAgree();");
            
            btnNotAgree.Visible = true;
        }
        else
        {
            this.Title = "::Update Login Information";
            literalMessage.Visible = false;
            trTermofService.Visible = false;
            trAgree.Visible = false;
            lblHeader.Text = "Update Login Information";
            txtEmail.Enabled = false;
            txtEmail.Text = Session["rcemail"].ToString();
            btnSave.Text = "Update";
          
            btnNotAgree.Visible = false;
            if (!IsPostBack)
                EditAccount();
        }
    }
    void EditAccount()
    {
        SqlServerProvider cls = new SqlServerProvider(connectionString);
        cls.CommandText = "select CompanyName,"
        + "EnglishName,AccountType, Newsletter,ResumeAlert," +
        "Promotion from tblRCAccounts" +
        " where RecruiterID='" + Session["rcuserid"] +
         "'";
        DataTable dataTable =
        cls.GetDataTable();
        txtCompanyName.Text = dataTable.Rows[0][0].ToString();
        txtEnglishName.Text = dataTable.Rows[0][1].ToString();
        if(dataTable.Rows[0][2].ToString() == "True")
            rdEmployer.Checked = true;
        else
            rdRecruiter.Checked = true;
        chkNewLetter.Checked = (dataTable.Rows[0][3].ToString() == "True");
        chkPromotion.Checked = (dataTable.Rows[0][4].ToString() == "True");
        chkResumeAlert.Checked = (dataTable.Rows[0][5].ToString() == "True");
    }
    string ClientScriptFunctionAgree()
    {
        string strScript = "";
        strScript =
        "function validateAgree()\n" +
        "{\n if(!document.getElementById('"
        + chkAgree.ID + "').checked){alert('"
        + "Please check Agree checkbox " +
        "if you agree our policy');" +
        "return false;} return true;" +
        " \n}\n";
        return strScript;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {


        if (txtEmail.Text != "")
        {
            SqlServerProvider cls = new SqlServerProvider(connectionString);
            if (Session["rcuserid"] == null)
            {
                string passWord = Session.SessionID.Substring(0, 10);
                Common common = new Common();
                passWord = common.RCVEncrypting(passWord, "rcv");
                string userID = cls.NewRCAccount(
                txtEmail.Text, passWord,              
                txtCompanyName.Text,
                txtEnglishName.Text,
                (rdEmployer.Checked) ? "0" : "1",
                (chkNewLetter.Checked) ? "1" : "0",
                (chkPromotion.Checked) ? "1" : "0",
                (chkResumeAlert.Checked) ? "1" : "0");
                if (cls.ErrorCode == 2627)
                {
                    literalError.Text =
                    "Sorry! RCV database show that " +
                    "someone has registered a job " +
                    "seeker account with this e-mail " +
                    "address. If you have previously " +
                    "registered under this e-mail " +
                    "address and forgotten your " +
                    "password, you can request for " +
                    "your password <a href=\"" +
                    "forgotpassword.aspx\">here</a>";
                }
                else 
                {
                    if (cls.ErrorMessage != "")
                    {
                        literalError.Text =
                        "Error: " + cls.ErrorMessage;
                    }
                    else
                    {
                        
                        MailClass mailClass = new MailClass();
                        mailClass.SendMail(txtEmail.Text,
                        "New Registration<br> Your password: " + 
                        passWord, "Thanks you for" +
                        " your registration at RecruitVietnam.com.");
                        Session.Add("rcuserid", userID);
                        Session.Add("rcemail", txtEmail.Text);
                        Session.Add("rcfullname",
                        txtCompanyName.Text + "-" + txtEnglishName.Text);
                        literalError.Text = "";
                        Response.Redirect("company.aspx");
                    }
                }
            }
            else
            {
                literalError.Text = "Updated successfull";
                cls.UpdateRCAccount(
                Session["rcuserid"].ToString(),
                txtEmail.Text,              
                txtCompanyName.Text,
                txtEnglishName.Text,
                (rdEmployer.Checked) ? "0" : "1",
                (chkNewLetter.Checked) ? "1" : "0",
                (chkPromotion.Checked) ? "1" : "0",
                (chkResumeAlert.Checked) ? "1" : "0");
                Session["rcfullname"] = txtCompanyName.Text
                + "-" + txtEnglishName.Text;
                Response.Redirect("company.aspx");
            }
            
        }
        else
        {
            literalError.Text =
            "Please check on Agree before submit";
        }
    }
}
